package org.jeecg.modules.demo.crm.glm;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.jeecg.modules.demo.crm.glm.util.HttpUtils;
import org.jeecg.modules.demo.crm.glm.util.JsonUtils;

import java.io.File;
import java.io.IOException;

import static org.jeecg.modules.demo.crm.glm.util.JsonUtils.*;

public class InvoiceParser {

    public static void main(String[] args) {
        try {
            String filePath = "https://img.remit.ee/api/file/BQACAgUAAyEGAASHRsPbAAIfJ2hL3M9K36er-4KNYbQTp89G-Zj3AAK3EwACrklhVrs9Ec1rqfTrNgQ.png";
            String requestBody = JsonUtils.createRequestJsonImages(filePath,fapiaoprompt);
            // 发送请求到GLM-4V-Flash API
            String response = HttpUtils.sendPostRequest(MODEL_URL, API_KEY, requestBody);
            JSONObject jsonObject = JSONObject.parseObject(response);
            System.out.println(jsonObject);
            if(jsonObject.get("choices") != null){
                JSONObject choices = jsonObject.getJSONArray("choices").getJSONObject(0);
                String content = choices.getJSONObject("message").getString("content");
                String json = content.replaceAll("```json","").replaceAll("```","");
                JSONArray data = JSONArray.parseArray(json);
                System.out.println(data);


            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    // 从PDF中提取文本内容
    public static String extractTextFromPDF(String filePath) {
        String text = "";
        try (PDDocument document = PDDocument.load(new File(filePath))) {
            PDFTextStripper pdfStripper = new PDFTextStripper();
            text = pdfStripper.getText(document);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return text;
    }

    // 预处理文本内容，合并换行信息
    public static String preprocessText(String text) {
        // 替换连续的换行符为一个空格
        text = text.replaceAll("\\n+", " ");
        // 替换多余的空格
        text = text.replaceAll("\\s+", " ");
        return text;
    }
}
